package cn.huiyunche.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.huiyunche.base.service.interfaces.BVehicleBrandService;
import cn.huiyunche.base.service.model.BVehicleBrand;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;

/**
 * 品牌Controller
 * @author lm
 *
 */
@Controller
@RequestMapping("/brand")
public class BrandController {
	private static final Logger LOGGER = LoggerFactory.getLogger(BrandController.class);
	
	@Autowired
	private BVehicleBrandService bVehicleBrandService = null;
	
	private BVehicleBrandService getBVehicleBrandService(){
		return this.bVehicleBrandService;
	} 
	
	/**
	 * 查询品牌列表
	 * @param pageVo
	 * @param bVehicleBrand
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<Object> getList(PageVo pageVo, BVehicleBrand bVehicleBrand){
		LOGGER.info("list params bVehicleBrand: {}, pageVo: {}", JSONObject.toJSONString(bVehicleBrand), pageVo.toString());
		Result<Object> result = new Result<Object>(true, null, "数据查询成功");
		result.setData(this.getBVehicleBrandService().getList(pageVo, bVehicleBrand));
		return result;
	}
	
	/**
	 * 修改或新增品牌列表
	 * @param bVehicleBrand
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Result<Integer> editBrand(BVehicleBrand bVehicleBrand){
		Result<Integer> result = new Result<>(true, "品牌添加成功");
		Long id = bVehicleBrand.getId();
		if(id != null){
			String brandName = bVehicleBrand.getBrandName();
			String brandNation = bVehicleBrand.getBrandNation();
			String brandPrefix = bVehicleBrand.getBrandPrefix();
			String brandLogo = bVehicleBrand.getBrandLogo();
			if(StringUtils.isBlank(brandName)||StringUtils.isBlank(brandLogo)||StringUtils.isBlank(brandPrefix)||StringUtils.isBlank(brandNation)){
				result.setSuccess(false);
				result.setMessage("必填参数不能为空");
				return result;
			} 
			result.setMessage("修改品牌信息成功");
		}
		result.setData(this.getBVehicleBrandService().editBrand(bVehicleBrand));
		return result;
	}

}